package com.ruoyi.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.system.domain.bo.EmgGoodsBo;
import com.ruoyi.system.domain.vo.EmgGoodsVo;
import com.ruoyi.system.domain.EmgGoods;
import com.ruoyi.system.mapper.EmgGoodsMapper;
import com.ruoyi.system.service.IEmgGoodsService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 仓库-物资Service业务层处理
 *
 * @author ruoyi
 * @date 2023-11-10
 */
@RequiredArgsConstructor
@Service
public class EmgGoodsServiceImpl implements IEmgGoodsService {

    private final EmgGoodsMapper baseMapper;

    /**
     * 查询仓库-物资
     */
    @Override
    public EmgGoodsVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询仓库-物资列表
     */
    @Override
    public TableDataInfo<EmgGoodsVo> queryPageList(EmgGoodsBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<EmgGoods> lqw = buildQueryWrapper(bo);
        Page<EmgGoodsVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询仓库-物资列表
     */
    @Override
    public List<EmgGoodsVo> queryList(EmgGoodsBo bo) {
        LambdaQueryWrapper<EmgGoods> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<EmgGoods> buildQueryWrapper(EmgGoodsBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<EmgGoods> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getWarehouseId() != null, EmgGoods::getWarehouseId, bo.getWarehouseId());
        lqw.eq(StringUtils.isNotBlank(bo.getClassId()), EmgGoods::getClassId, bo.getClassId());
        lqw.like(StringUtils.isNotBlank(bo.getName()), EmgGoods::getName, bo.getName());
        lqw.eq(StringUtils.isNotBlank(bo.getSpec()), EmgGoods::getSpec, bo.getSpec());
        lqw.eq(bo.getQuantity() != null, EmgGoods::getQuantity, bo.getQuantity());
        lqw.eq(StringUtils.isNotBlank(bo.getUnit()), EmgGoods::getUnit, bo.getUnit());
        lqw.eq(bo.getPrice() != null, EmgGoods::getPrice, bo.getPrice());
        lqw.eq(StringUtils.isNotBlank(bo.getAddress()), EmgGoods::getAddress, bo.getAddress());
        lqw.eq(StringUtils.isNotBlank(bo.getStatus()), EmgGoods::getStatus, bo.getStatus());
        return lqw;
    }

    /**
     * 新增仓库-物资
     */
    @Override
    public Boolean insertByBo(EmgGoodsBo bo) {
        EmgGoods add = BeanUtil.toBean(bo, EmgGoods.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改仓库-物资
     */
    @Override
    public Boolean updateByBo(EmgGoodsBo bo) {
        EmgGoods update = BeanUtil.toBean(bo, EmgGoods.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(EmgGoods entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除仓库-物资
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
